﻿using PhotoKiosk.Properties;
using PhotoKiosk.ViewModel.Helpers;
using System.Resources;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel.UserControls
{
    public class NumbersPadAndTextViewModel : ViewModelBase
    {
        public event Handler OnNumberChanged;
        public delegate void Handler(string i_Number);

        private DelegateCommand m_pad1PressCommand;
        private DelegateCommand m_pad2PressCommand;
        private DelegateCommand m_pad3PressCommand;
        private DelegateCommand m_pad4PressCommand;
        private DelegateCommand m_pad5PressCommand;
        private DelegateCommand m_pad6PressCommand;
        private DelegateCommand m_pad7PressCommand;
        private DelegateCommand m_pad8PressCommand;
        private DelegateCommand m_pad9PressCommand;
        private DelegateCommand m_pad0PressCommand;
        private DelegateCommand m_padSeparatorPressCommand;
        private DelegateCommand m_padDeletePressCommand;
        private string m_number;
        public string Number
        {
            get
            {
                if (m_number == null)
                {
                    m_number = string.Empty;
                }
                return m_number;
            }
            set
            {
                if (m_number != value)
                {
                    m_number = value;
                    checkInput();
                    if (OnNumberChanged != null)
                    {
                        OnNumberChanged(m_number);
                    }
                    OnPropertyChanged("Number");
                }
            }
        }

        public string Pad1PressCommandText
        {
            get
            {
                return "1";
            }
        }
        public string Pad2PressCommandText
        {
            get
            {
                return "2";
            }
        }
        public string Pad3PressCommandText
        {
            get
            {
                return "3";
            }
        }
        public string Pad4PressCommandText
        {
            get
            {
                return "4";
            }
        }
        public string Pad5PressCommandText
        {
            get
            {
                return "5";
            }
        }
        public string Pad6PressCommandText
        {
            get
            {
                return "6";
            }
        }
        public string Pad7PressCommandText
        {
            get
            {
                return "7";
            }
        }
        public string Pad8PressCommandText
        {
            get
            {
                return "8";
            }
        }
        public string Pad9PressCommandText
        {
            get
            {
                return "9";
            }
        }
        public string Pad0PressCommandText
        {
            get
            {
                return "0";
            }
        }
        public string PadSeparatorPressCommandText
        {
            get
            {
                return "-";
            }
        }

        public ICommand Pad1PressCommand
        {
            get
            {
                if (m_pad1PressCommand == null)
                {
                    m_pad1PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad1PressCommandText));
                }
                return m_pad1PressCommand;
            }
        }
        public ICommand Pad2PressCommand
        {
            get
            {
                if (m_pad2PressCommand == null)
                {
                    m_pad2PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad2PressCommandText));
                }
                return m_pad2PressCommand;
            }
        }
        public ICommand Pad3PressCommand
        {
            get
            {
                if (m_pad3PressCommand == null)
                {
                    m_pad3PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad3PressCommandText));
                }
                return m_pad3PressCommand;
            }
        }
        public ICommand Pad4PressCommand
        {
            get
            {
                if (m_pad4PressCommand == null)
                {
                    m_pad4PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad4PressCommandText));
                }
                return m_pad4PressCommand;
            }
        }
        public ICommand Pad5PressCommand
        {
            get
            {
                if (m_pad5PressCommand == null)
                {
                    m_pad5PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad5PressCommandText));
                }
                return m_pad5PressCommand;
            }
        }
        public ICommand Pad6PressCommand
        {
            get
            {
                if (m_pad6PressCommand == null)
                {
                    m_pad6PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad6PressCommandText));
                }
                return m_pad6PressCommand;
            }
        }
        public ICommand Pad7PressCommand
        {
            get
            {
                if (m_pad7PressCommand == null)
                {
                    m_pad7PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad7PressCommandText));
                }
                return m_pad7PressCommand;
            }
        }
        public ICommand Pad8PressCommand
        {
            get
            {
                if (m_pad8PressCommand == null)
                {
                    m_pad8PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad8PressCommandText));
                }
                return m_pad8PressCommand;
            }
        }
        public ICommand Pad9PressCommand
        {
            get
            {
                if (m_pad9PressCommand == null)
                {
                    m_pad9PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad9PressCommandText));
                }
                return m_pad9PressCommand;
            }
        }
        public ICommand Pad0PressCommand
        {
            get
            {
                if (m_pad0PressCommand == null)
                {
                    m_pad0PressCommand = new DelegateCommand(param => this.padPressCommandExecute(Pad0PressCommandText));
                }
                return m_pad0PressCommand;
            }
        }
        public ICommand PadSeparatorPressCommand
        {
            get
            {
                if (m_padSeparatorPressCommand == null)
                {
                    m_padSeparatorPressCommand = new DelegateCommand(param => this.padPressCommandExecute(PadSeparatorPressCommandText));
                }
                return m_padSeparatorPressCommand;
            }
        }
        public ICommand PadDeletePressCommand
        {
            get
            {
                if (m_padDeletePressCommand == null)
                {
                    m_padDeletePressCommand = new DelegateCommand(param => this.padDeletePressCommandExecute(), param => this.padDeletePressCommandCanExecute());
                }
                return m_padDeletePressCommand;
            }

        }

        private bool padDeletePressCommandCanExecute()
        {
            return !Number.Equals(string.Empty);
        }

        private void padDeletePressCommandExecute()
        {
            Number = Number.Remove(Number.Length - 1);
        }

        private void padPressCommandExecute(string i_input)
        {
            Number += i_input;
        }

        public string m_error = string.Empty;
        public string Error
        {
            get
            {
                return m_error;
            }
            set
            {
                m_error = value;
                OnPropertyChanged("Error");
                OnPropertyChanged("ErrorColor");
            }
        }

        public bool ErrorColor
        {
            get
            {
                return !string.IsNullOrEmpty(Error);
            }
        }

        private void checkInput()
        {
            Error = string.Empty;
            if (string.IsNullOrEmpty(Number))
            {
                ResourceManager myManager = new ResourceManager(typeof(Resources));
                Error = myManager.GetString("Text_Error_Field_Required"); 
            }
        }

        public bool IsValidNumber()
        {
            checkInput();
            return string.IsNullOrEmpty(Error);
        }
    }
}
